package lemna.top.chunyu.batch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import lemna.top.chunyu.batch.JobCompletionNotificationListener;
import lemna.top.chunyu.batch.reader.CYFlatFileItemReader;
import lemna.top.chunyu.batch.step1.Step1ItemProcessor;
import lemna.top.chunyu.batch.step1.Step1Writer;
import lemna.top.chunyu.batch.step1.domain.ChunYuPaymentStatement;
import lemna.top.chunyu.batch.step2.Step2ItemProcessor;
import lemna.top.chunyu.batch.step2.Step2Writer;
import lemna.top.chunyu.batch.step2.domain.AlipayPaymentStatement;
import lemna.top.chunyu.batch.step3.Step3ItemProcessor;
import lemna.top.chunyu.batch.step3.Step3Writer;
import lemna.top.chunyu.batch.step3.domain.WeiXiPaymentStatement;
import lemna.top.chunyu.batch.step4.Step4ItemProcessor;
import lemna.top.chunyu.batch.step4.domain.CommPaymentStatement;


@Configuration
@EnableBatchProcessing(modular=false)
public class BatchConfiguration {

	@Autowired
	public JobBuilderFactory jobBuilderFactory;
	
	@Autowired
	public StepBuilderFactory stepBuilderFactory;
	
	@Autowired
	public CYFlatFileItemReader<ChunYuPaymentStatement> step1Reader;
	
	@Autowired
	public FlatFileItemReader<AlipayPaymentStatement> step2Reader;
	
	@Autowired
	public CYFlatFileItemReader<WeiXiPaymentStatement> step3Reader;

	@Autowired
	JobCompletionNotificationListener listener;
	
	@Autowired
	Step step4;

	@Autowired
	Step step5;

	@Autowired
	Step step6;

	@Autowired
	Step step7;

	@Autowired
	Step step8;

	@Bean
	public ResourcelessTransactionManager resourcelessTransactionManager() {
	    return new ResourcelessTransactionManager();
	}

	@Bean
	public JobRepository jobRepository() throws Exception {
	    return new MapJobRepositoryFactoryBean(resourcelessTransactionManager()).getObject();
	}
	


	@Bean
	public Step1ItemProcessor step1Processor() {
		
		return new Step1ItemProcessor();
	}

	@Bean
	public ItemWriter<ChunYuPaymentStatement> step1Writer() throws Exception {
		return new Step1Writer();
	}

	@Bean
	public Job importUserJob() throws Exception {
//		return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).flow(step1()).next(step2()).next(step3()).next(step4()).end().build();
		return jobBuilderFactory.get("importUserJob").listener(listener).incrementer(new RunIdIncrementer()).flow(step4).next(step5).next(step6).next(step7).next(step8).end().build();
	}

	@Bean
	public Step step1() throws Exception {
		return stepBuilderFactory.get("step1").<ChunYuPaymentStatement, ChunYuPaymentStatement>chunk(1).reader(step1Reader).processor(step1Processor())
				.writer(step1Writer()).build();
	}


	@Bean
	public Step2ItemProcessor step2Processor() {
		return new Step2ItemProcessor();
	}

	@Bean
	public ItemWriter<AlipayPaymentStatement> step2Writer() throws Exception {
		return new Step2Writer();
	}

	@Bean
	public Step step2() throws Exception {
		return stepBuilderFactory.get("step2").<AlipayPaymentStatement, AlipayPaymentStatement>chunk(1).reader(step2Reader).processor(step2Processor())
				.writer(step2Writer()).build();
	}
	

	@Bean
	public Step3ItemProcessor step3Processor() {
		return new Step3ItemProcessor();
	}

	@Bean
	public ItemWriter<WeiXiPaymentStatement> step3Writer() throws Exception {
		return new Step3Writer();
	}

	@Bean
	public Step step3() throws Exception {
		return stepBuilderFactory.get("step3").<WeiXiPaymentStatement, WeiXiPaymentStatement>chunk(1).reader(step3Reader).processor(step3Processor())
				.writer(step3Writer()).build();
	}


}